var myWidth = 0, myHeight = 0, myContent=0,contentHeight=0;
$(document).ready(function () {
    loadSize();
    $('.sub-item').hide();
    loadContext();
    $('.iframe').click(function () {
        var id = $('.grid table tr.context-menu-active').attr('id');
        if ($(this).hasClass('btn-add')) {
            id = null;
        } else {
            if (id == null) {
                alert('Bạn chưa chọn dữ liệu!');
                return false;
            }
        }
        var hrf = $(this).attr('href'); // +'&id=' + id; /add-units.htm&id=null
        var dimension = $(this).attr('data-dimension').split('|');
        $(".iframe").colorbox({
            iframe: true,
            href: hrf,
            escKey: true,
            overlayClose: false,
            closeButton: false,
            innerWidth: dimension[0],
            innerHeight: dimension[1]
        });
        return true;
    });
    initActionGrid();
});

function initActionGrid() {
    $('.grid table tr').click(function () {
        $('.grid table tr').removeClass('context-menu-active');
        $(this).addClass('context-menu-active');
    });
    $('.grid table tr').dblclick(function () {
        $('.btn-edit').click();
    });
    $('.grid table').mousedown(function (e) {
        if (e.button == 2) { //2 for Right Click, 3 for mouse wheel click
            $('.grid tr').removeClass('context-menu-active');
            return false;
        }
        return true;
    });
}
function onReload() {
    $('.btn-refresh').click();
}
function closeColorBox(){
    if(confirm('Bạn có chắc chắn muốn thoát?')){
        //window.location.reload(true);
        $.colorbox.close();
    }
}
function closeColorBoxNotConfirm() {
    //if (confirm('Bạn có chắc chắn muốn thoát?')) {
    //window.location.reload(true);
   // window.location.reload(true);
    $.colorbox.close();
  window.location.reload(true);
   // }
}
function maximumColorBox(){
    $.colorbox.resize({width:'100%',height:'100%'}); 
}
function minimumColorBox(width,height){
    $.colorbox.resize({innerWidth:width,innerHeight:height}); 
}
function loadContext(){
    $('.grid').contextMenu({
        selector: 'tr', 
        callback: function(key, options) {
            $(this).addClass('context-menu-active');
            var action = options.items[key].button;
            $('a.'+action).click();
        },
        items: {
            "add": {name: "Thêm", icon: "add", button:"btn-add"},
            "edit": {name: "Sửa", icon: "edit", button:"btn-edit"},
            "delete": {name: "Xóa", icon: "delete",button:"btn-delete"},
            "active": {name: "Kích hoạt", icon: "active", button:"btn-active"},
            "grant-unit": {name: "Gán đơn vị", icon: "grant-unit", button:"btn-grant-unit"},
            "role": {name: "Gán nhóm quyền", icon: "grant-role", button:"btn-add-role"},
            "sep1": "---------",
            "quit": {name: "Quit", icon: "quit"}
        }
    });
     
}
function onMenuContextClick(hrf) {
    
}
function loadSize(){    
            if (typeof (window.innerWidth) == 'number') {
                //Non-IE
                myWidth = window.innerWidth;
                myHeight = window.innerHeight;
            } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
                //IE 6+ in 'standards compliant mode'
                myWidth = document.documentElement.clientWidth;
                myHeight = document.documentElement.clientHeight;
            } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
                //IE 4 compatible
                myWidth = document.body.clientWidth;
                myHeight = document.body.clientHeight;
            }
            myContent=$('.line').height()+$('.banner').height()+$('.footer').height();
            contentHeight=myHeight-myContent;
            $('.wrapper').css('height',myHeight);
            $('#content').css('height',contentHeight-1);
            var liSize=$('ul.root >li').size();
            var liHeight=$('ul.root >li').height();
            var ulSize=liSize*liHeight;
            $('ul.root li ul').css('height',contentHeight-ulSize);
            $('.scroll .viewport').css('height',contentHeight-ulSize);
            $('#scrollbar2 .viewport').css('height',contentHeight);
            $('.scroll').tinyscrollbar(); 
            $('#scrollbar2').tinyscrollbar(); 
            loadNav(contentHeight-ulSize);
            loadGrid();
}
function loadNav(h){
	$("ul.root li").on('click',function() {
	    $("ul.root li").stop().animate({ height: '40px' }, { queue: false, duration: 600, easing: 'easeOutBounce' });
	    $(this).stop().animate({ height: h + 35 }, { queue: false, duration: 600, easing: 'easeOutBounce' });
        $('ul.root li').removeClass('active');
        $(this).addClass('active');
	    $(this).find('.sub-item').show();
	});
}
function loadGrid(){
    $('.grid .bottom').height(contentHeight-($('.grid .title').height()+$('.grid .top').height()+$('.grid .pagging').height()));
    //$('.bottom table tr:first').css({'position':'fixed','top':'170px','width':'100%'});
}
function loadShare() {
    var m1 = 170;
    var m2 = 0;
    var p = document.getElementById('box');
    if (p != null) {
        function m() {
            var top = Math.max(document.body.scrollTop, document.documentElement.scrollTop);
            if (top + m2 < m1) {
                p.style.top = m1 - top + 'px';
                $(p).css({ 'box-shadow': '0px 0px 0px #A59E94' });
            }
            else {
                p.style.top = m2 + 'px';
                $(p).css({ 'box-shadow': '0px 5px 5px #A59E94' });
            }
        }
        m();
        window.onscroll = m;
    }
}